{include file="../application/admin/view/common/header.html" /}
<!--main content start-->
<section class="main-content">


    <!--start page content-->

    <div class="row">

        <div class="col-md-12">
            <div class="panel panel-default collapsed">
                <div class="panel-heading">
                    <a class="btn btn-danger ui-w" onclick="role_del()" href="javascript:void(0);"><span
                            class="ui-icon ui-icon-trash"></span>批量删除</a>
                    <a class="btn btn-blue ui-w" onclick="role_add(this)" href="javascript:void(0);"><span
                            class="ui-icon ui-icon-plus"></span>添加角色</a>
                    <div class="search-form">
                        <input type="text" class="form-control" placeholder="搜索角色名称"/>
                        <button type="button" id="submit" class="btn-search"><i class="fa fa-search"></i></button>
                    </div>
                </div>
                <div class="panel-body">

                    <table class="table table-hover table-striped table-bordered" data-page-size="12"
                           data-filter=#filter>
                        <thead>
                        <tr>
                            <th>选择</th>
                            <th>ID</th>
                            <th>角色名称</th>
                            <th>权限描述</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>


                        </tbody>
                    </table>
                    <div class="pager" id="page" style="text-align: right;"></div>
                </div>
            </div>
        </div>
    </div><!--end row-->

    <!--end page content-->


</section>
<!--end main content-->


<!--Common plugins-->
<script src="_JS_/jquery.min.js"></script>
<script src="_JS_/jquery.base64.js"></script>
<script src="_JS_/jquery.cookie.js"></script>
<script src="_JS_/bootstrap.min.js"></script>
<script src="_JS_/jasny-bootstrap.min.js"></script>
<script src="_JS_/jquery.slimscroll.min.js"></script>
<script src="_JS_/jquery.nanoscroller.min.js"></script>
<script src="_JS_/metismenu.min.js"></script>
<script src="_JS_/float-custom.js"></script>
<script type="text/javascript" src="_JS_/layer/layer.js"></script>
<script type="text/javascript" src="_JS_/laypage/laypage.js"></script>
<script type="text/javascript" src="_JS_/laydata/laydate.js"></script>
<script type="text/javascript" src="_JS_/getMyDate.js"></script>
<script>

    $(function () {
        $("#page-title").html("权限管理");
        postfile(1);
    })

    function postfile(page) {
        var username = $(".search-form input").val();
        $.ajax({
            type: 'GET',
            url: "{:url('api/adminRole/role_list')}",
            dataType: 'jsonp',
            jsonp: 'callback',
            data: {'page': page, 'username': username},
            beforeSend: function () {
                var index = layer.load(1, {
                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                });
            },
            success: function (msg) {
                layer.closeAll();
                $("#admin_management").addClass("active").children("ul").addClass("in");
                $("#admin_role").addClass("choose");
                if (msg.code == 200) {
                    var pages = Math.ceil(msg.data.count / 10);
                    $(".table tbody").html("");
                    if (msg.data.data.length > 0) {
                        for (var i = 0; i < msg.data.data.length; i++) {

                            var time = msg.data.data[i].time;
                            ltime = getMyDate(time * 1000);
                            var html = '<tr id="' + msg.data.data[i].id + '"><td><input type="checkbox" name="allcheck"></td>' +
                                '<td>' + msg.data.data[i].id + '</td><td>' + msg.data.data[i].name + '</td>' +
                                '<td>' + msg.data.data[i].desc + '</td><td class="buttons td-manage">' +
                                '<a class="btn table-btn" onclick="role_edit(this)" href="#" title="编辑"><i class="fa fa-pencil"></i></a>' +
                                '<a class="btn table-btn" onclick="admin_del(this,' + msg.data.data[i].id + ')" href="#" title="删除"><i class="fa fa-trash-o"></i>' +
                                '</a></td></tr>';

                            $(".table tbody").append(html);
                        }
                    }
                    //分页
                    $(".table").attr("pages", page);
                    laypage({
                        cont: 'page',
                        pages: pages,
                        curr: page,
                        jump: function (obj, first) {
                            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                postfile(obj.curr);
                            }
                        }
                    })
                } else {
                    layer.msg(msg.message, {icon: 5, time: 2000});
                }
            }
        })
    }

    $('#submit').click(function () {
        postfile(1);
    });

    //回车键绑定搜索
    $(".search-form").keydown(function (event) {
        if (event.keyCode == 13) {
            postfile(1);
        }
    });

    /*批量删除*/
    function role_del(obj) {
        var arrs = new Array();
        $(".table input[name='allcheck']:checked").each(function () {
            arrs.push($(this).parents('tr').attr('id'));
        });
        if (arrs.length == 0) {
            layer.msg('请选择删除项!', {icon: 1, time: 1000});
            return false;
        }
        var uids = arrs.join(",");
        layer.confirm('确认要删除多个角色吗？', function (index) {
            var pages_num = $(".table").attr("pages");
            var name = 'all';
            $.ajax({
                type: "get",
                url: "{:url('api/adminRole/delete')}",
                dataType: 'jsonp',
                jsonp: 'callback',
                data: {'id': uids, 'name': name},
                success: function (msg) {
                    if (msg.code == 200) {
                        // $(obj).parents('tr').remove();
                        postfile(pages_num);
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    }
                    if (msg.code == 1000) {
                        layer.msg(msg.message, {icon: 5, time: 1000});
                    }
                }
            });
        });
    }

    /*添加管理员*/
    function role_add(obj) {
        var page_id = "#" + $(".laypage_curr").html();
        layer.open({
            type: 2,
            title: '添加角色',
            shadeClose: true,
            shade: 0.8,
            area: ['800px', '750px'],
            content: "{:url('role_add')}" + page_id
        });
    }

    /* 编辑*/
    function role_edit(obj) {
        var page_id = "#" + $(".laypage_curr").html();
        var id = "#" + $(obj).parents('tr').attr('id');
        layer.open({
            type: 2,
            title: '编辑',
            shadeClose: true,
            shade: 0.8,
            area: ['800px', '750px'],
            content: "{:url('role_edit')}" + page_id + id
        });
    }

    /*管理员-删除*/
    function admin_del(obj, id) {
        var pages_num = $(".table").attr("pages");
        layer.confirm('确认要删除吗？', function (index) {
            //此处请求后台程序，下方是成功后的前台处理……
            $.ajax({
                type: 'get',
                url: "{:url('api/adminRole/delete')}",
                dataType: 'jsonp',
                jsonp: 'callback',
                data: {'id': id},
                success: function (msg) {
                    if (msg.code == 200) {
                        postfile(pages_num);
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    } else {
                        layer.msg(msg.message, {icon: 5, time: 2000});
                    }
                }
            })
        });
    }


</script>
</body>
</html>